<template>
  <view class="">
    <!-- 头部搜索链接 -->
    <SearchLink />

    <!-- 轮播图 -->
    <swiper
      autoplay
      circular
      indicator-dots
      indicator-active-color="#fff"
      indicator-color="rgba(255,255,255,0.3)"
    >
      <swiper-item v-for="(item, index) in swiperdata" :key="index">
        <image :src="item.image_src"></image>
      </swiper-item>
    </swiper>

    <!-- 导航区 -->
    <view class="nav">
      <image
        v-for="(item, index) in catitems"
        :src="item.image_src"
        :key="index"
      ></image>
    </view>
    <!-- 楼层 -->
    <view class="floor">
      <view class="floor-item" v-for="(floor, findex) in floordata" :key="findex">
        <image :src="floor.floor_title.image_src"></image>
        <view class="product-list">
          <image :src="floor.product_list[0].image_src"></image>
          <view class="right">
            <block v-for="(item, index) in floor.product_list" :key="index">
              <image v-if="index" :src="item.image_src"></image>
            </block>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import SearchLink from '@/components/SearchLink'

export default {
  components: {
    // SearchLink:SearchLink
    SearchLink
  },
  data () {
    return {
      // 轮播图数据
      swiperdata: [],
      // 导航栏数组
      catitems: [],
      // 楼层数组
      floordata: []
    }
  },
  onShow () {
    // 设置购物车右上角的文本
    wx.setTabBarBadge({
      index: 2,
      text: (this.$store.getters.getCart || []).length + ''
    })
  },
  onLoad () {
    this.getSwiperdata()
    this.getCatitems()
    this.getFloordata()
  },
  methods: {
    async getSwiperdata () {
      this.swiperdata = await this.$request({
        url: '/api/public/v1/home/swiperdata'
      })
    },
    async getCatitems () {
      this.catitems = await this.$request({
        url: '/api/public/v1/home/catitems'
      })
    },
    async getFloordata () {
      try {
        this.floordata = await this.$request({
          url: '/api/public/v1/home/floordata'
        })
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style lang="less">
swiper {
  height: 340rpx;

  image {
    height: 340rpx;
    width: 100%;
  }
}

.nav {
  height: 194rpx;
  display: flex;
  align-items: center;
  justify-content: space-around;

  image {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-item {
  > image {
    width: 100%;
    height: 88rpx;
  }
}

.product-list {
  display: flex;
  padding: 20rpx 17rpx 0;

  > image {
    width: 232rpx;
    height: 386rpx;
  }

  .right {
    flex: 1;
    display: flex;
    flex-wrap: wrap;

    image {
      width: 232rpx;
      height: 188rpx;
      margin: 0 0 10rpx 10rpx;
    }
  }
}
</style>
